package cn.ming.wfchat.app.me.activity;

import android.os.Handler;
import android.text.Editable;
import android.widget.Button;
import android.widget.EditText;
import android.widget.FrameLayout;
import android.widget.TextView;
import android.widget.Toast;

import butterknife.BindView;
import butterknife.OnClick;
import butterknife.OnTextChanged;
import cn.ming.wfchat.R;
import cn.ming.wfchat.app.AppService;
import cn.ming.wfchat.kit.WfcBaseActivity;
import cn.wildfirechat.remote.ChatManager;

/**
 * @author luoming
 * created at 2020/9/7 3:15 PM
 * 设置手机号
 */
public class SetSjhActivity extends WfcBaseActivity {

    @BindView(R.id.loginButton)
    Button loginButton;
    @BindView(R.id.phoneNumberEditText)
    TextView phoneNumberEditText;
    @BindView(R.id.authCodeEditText)
    EditText authCodeEditText;
    @BindView(R.id.requestAuthCodeButton)
    TextView requestAuthCodeButton;
    @BindView(R.id.fl_authcode)
    FrameLayout flAuthcode;
    @BindView(R.id.new_phoneNumberEditText)
    EditText newPhoneNumberEditText;
    @BindView(R.id.new_authCodeEditText)
    EditText newAuthCodeEditText;
    @BindView(R.id.new_requestAuthCodeButton)
    TextView newRequestAuthCodeButton;

    @Override
    protected int contentLayout() {
        return R.layout.activity_set_sjh;
    }

    @Override
    protected void afterViews() {
        setTitle("手机号");
        phoneNumberEditText.setText(ChatManager.Instance().getUserInfo(ChatManager.Instance().getUserId(), false).mobile);
    }

    private Handler handler = new Handler();

    @Override
    protected void onDestroy() {
        super.onDestroy();
    }

    @OnClick(R.id.requestAuthCodeButton)
    void requestAuthCode() {
        requestAuthCodeButton.setEnabled(false);
        handler.postDelayed(new Runnable() {
            @Override
            public void run() {
                if (!isFinishing()) {
                    requestAuthCodeButton.setEnabled(true);
                }
            }
        }, 60 * 1000);

        Toast.makeText(this, "请求验证码...", Toast.LENGTH_SHORT).show();
        String phoneNumber = phoneNumberEditText.getText().toString().trim();

        AppService.Instance().sendOldMobile(phoneNumber, new AppService.SendCodeCallback() {
            @Override
            public void onUiSuccess() {
                Toast.makeText(SetSjhActivity.this, "发送验证码成功", Toast.LENGTH_SHORT).show();
            }

            @Override
            public void onUiFailure(String msg) {
                Toast.makeText(SetSjhActivity.this, "发送验证码失败: " + " " + msg, Toast.LENGTH_SHORT).show();
            }
        });
    }

    @OnClick(R.id.new_requestAuthCodeButton)
    void newrequestAuthCode() {
        newRequestAuthCodeButton.setEnabled(false);
        handler.postDelayed(new Runnable() {
            @Override
            public void run() {
                if (!isFinishing()) {
                    newRequestAuthCodeButton.setEnabled(true);
                }
            }
        }, 60 * 1000);

        Toast.makeText(this, "请求验证码...", Toast.LENGTH_SHORT).show();
        String phoneNumber = newPhoneNumberEditText.getText().toString().trim();

        AppService.Instance().sendNewMobile(phoneNumber, new AppService.SendCodeCallback() {
            @Override
            public void onUiSuccess() {
                Toast.makeText(SetSjhActivity.this, "发送验证码成功", Toast.LENGTH_SHORT).show();
            }

            @Override
            public void onUiFailure(String msg) {
                Toast.makeText(SetSjhActivity.this, "发送验证码失败: " + " " + msg, Toast.LENGTH_SHORT).show();
            }
        });
    }


    @OnTextChanged(value = R.id.authCodeEditText, callback = OnTextChanged.Callback.AFTER_TEXT_CHANGED)
    void inputAuthCode(Editable editable) {
        if (editable.toString().length() > 2) {
            loginButton.setEnabled(true);
        }
    }


    @OnClick(R.id.loginButton)
    void login() {
        String authCode = authCodeEditText.getText().toString().trim();
        String newphoneNumber = newPhoneNumberEditText.getText().toString().trim();
        String newauthCode = newAuthCodeEditText.getText().toString().trim();
        loginButton.setEnabled(false);
        if (newauthCode.length() < 3) {
            ChatManager.showToast("请输入新手机号验证码");
        }
        AppService.Instance().replaceNewMobile(newphoneNumber, newauthCode, authCode, new AppService.SendCodeCallback() {
            @Override
            public void onUiSuccess() {
                if (isFinishing()) {
                    return;
                }
                ChatManager.showToast("操作成功");
                finish();
                AppService.Instance().outExit();
            }

            @Override
            public void onUiFailure(String msg) {
                if (isFinishing()) {
                    return;
                }
                ChatManager.showToast(msg);
                loginButton.setEnabled(true);
            }
        });
    }

}
